
import orm
import time
def show_bill():
    path = r'cashbook.json'
    book_list = orm.read_data(path)
    # for book in book_list:
    #     print(f"time: {book['time']}  project: {book['project']}  assort: {book['assort']}  amount: {book['amount']}")
    total_money = 0
    total_expend = 0
    for book in book_list:
        amount = float(book["amount"])
        # print(amount)
        if amount > 0:
            total_money += amount
        elif amount < 0:
            total_expend -= amount
    print(f'总收入金额为：{total_money}')
    print(f'总支出金额为：{total_expend}')
    print(f'结余金额为：{total_money - total_expend}')

def write_bill():
    path = r'cashbook.json'
    now_time = time.strftime('%Y-%m-%d %H:%M:%S')
    project = input('请输入项目名称：')
    assort = input('请输入分类名称：')
    amount = input('请输入金额：')
    new_data = {'time': now_time,'project': project, 'assort': assort, 'amount': amount}
    book_list = orm.read_data(path)
    book_list.append(new_data)
    orm.write_data(book_list, path)
if __name__ == '__main__':
    show_bill()
    # init_data = [{"time": "2021/8/21 09:00:00", "project": "工资", "assort": "收入", "amount": "20000"},
    #              {"time": "2021/8/21 09:00:00", "project": "聚餐", "assort": "支出", "amount": "-200"},
    #              ]
    # orm.write_data(init_data, r'cashbook.json')
    # 测试写入账单功能
    #write_bill()
